package org.jeecg.modules.engineering.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.modules.engineering.entity.EquipEventSort;
import org.jeecg.modules.engineering.entity.EquipEventSortDetail;
import org.jeecg.modules.engineering.mapper.EquipEventSortDetailMapper;
import org.jeecg.modules.engineering.mapper.EquipEventSortMapper;
import org.jeecg.modules.engineering.service.IEquipEventSortDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 每个小区的事件分类
 * @Author: jeecg-boot
 * @Date:   2019-09-11
 * @Version: V1.0
 */
@Service
public class EquipEventSortDetailServiceImpl extends ServiceImpl<EquipEventSortDetailMapper, EquipEventSortDetail> implements IEquipEventSortDetailService {


    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private EquipEventSortDetailMapper equipEventSortDetailMapper;

    @Override
    public void saveWithRedis(EquipEventSortDetail entity) {
        //存储到数据库
        boolean result = save(entity);
        if(result){
            //以ID为key，存储到redis
            setToRedis(entity);
        }
    }

    @Override
    public boolean updateWithRedis(EquipEventSortDetail entity) {
        EquipEventSortDetail oldEntity = getById(entity.getId());
        boolean result = updateById(entity);
        if(result){
            //先移除编辑前的redis键值
            removeFromRedis(oldEntity);
            //更新成功后，重新设置redis
            setToRedis(entity);
        }
        return result;
    }

    @Override
    public void removeWithRedis(String id) {
        EquipEventSortDetail entity = getById(id);
        //从数据库删除
        boolean result = removeById(id);
        if(result){
            //从redis删除
            removeFromRedis(entity);
        }
    }

    private void setToRedis(EquipEventSortDetail entity){
        if(entity == null){
            return;
        }
        redisUtil.set(CacheConstant.REDIS_TABLES+"equip_event_sort_detail:id:"+entity.getId(), JSONObject.toJSONString(entity));
        redisUtil.set(CacheConstant.REDIS_TABLES+"equip_event_sort_detail:otherKey:"+entity.getProjectId()+"_"+entity.getEventSortId(),entity.getId());
    }

    @Override
    public void removeFromRedis(String id){
        removeFromRedis(findById(id));
    }

    @Override
    public void removeFromRedis(EquipEventSortDetail entity){
        if(entity == null){
            return;
        }
        redisUtil.del(CacheConstant.REDIS_TABLES+"equip_event_sort_detail:id:"+entity.getId()
                ,CacheConstant.REDIS_TABLES+"equip_event_sort_detail:otherKey:"+entity.getProjectId()+"_"+entity.getEventSortId()
        );
    }


    @Override
    public EquipEventSortDetail getFromRedisByOtherKey(String key){
        String id = (String) redisUtil.get(CacheConstant.REDIS_TABLES+"equip_event_sort_detail:otherKey:"+key);
        if(StringUtils.isEmpty(id)){
            return null;
        }
        return getFromRedisById(id);
    }

    @Override
    public EquipEventSortDetail getFromRedisById(String id){
        EquipEventSortDetail entity = JSONObject.parseObject((String) redisUtil.get(CacheConstant.REDIS_TABLES+"equip_event_sort_detail:id:"+id),EquipEventSortDetail.class) ;
        if(entity == null){
            //从数据库查询
            entity = findById(id);
            if(entity != null){
                //重新设置redis
                setToRedis(entity);
            }
        }
        return entity;
    }

    @Override
    public Result<String> refreshToRedis() {
        List<EquipEventSortDetail> lists = list();
        if(lists != null && lists.size() > 0){
            for (EquipEventSortDetail entity : lists){
                setToRedis(entity);
            }
        }
        return Result.ok();
    }

    @Override
    public List<EquipEventSortDetail> queryListByProjectId(String projectId) {
        return equipEventSortDetailMapper.queryListByProjectId(projectId);
    }

    @Override
    public void batchRemoveWithRedis(List<String> ids) {
        if(ids != null && ids.size() > 0){
            for (String id :ids){
                removeWithRedis(id);
            }
        }
    }

    @Override
    public boolean checkHave(String projectId, String eventId, String id) {
        long count = equipEventSortDetailMapper.getCount(projectId,eventId,id);
        return count > 0;
    }

    private EquipEventSortDetail findById(String id) {
        return equipEventSortDetailMapper.findById(id);
    }


}
